﻿using System;
using System.Collections.Generic;

namespace SeaBattle.Domain.Entities
{
    public class Game
    {
        public Game(int id, string name, DateTime created, Player creator)
        {
            Id = id;
            Name = name;
            Created = created;
            Creator = creator;
            State = GameState.StateOpened;
        }

        private Player _opponent;

        public int Id { get; private set; }
        public string Name { get; set; }
        public int Preset { get; set; }
        public DateTime Created { get; private set; }
        public Player Creator { get; private set; }
        public Player Opponent { get { return _opponent; } set { SetOpponent(value); } }
        public GameState State { get; private set; }

        private void SetOpponent(Player theOpponent)
        {
            if (!Opened)
            {
                throw new Exception("The game " + Id + " not opened");
            }
            if (Creator == theOpponent)
            {
                throw new Exception("The opponent is the same as creator of the game " + Id);
            }
            _opponent = theOpponent;
        }

        public void JoinOpponent(Player theOpponent)
        {
            Opponent = theOpponent;
            BoardPreparing = true;
        }

        public bool Opened
        {
            get { return State == GameState.StateOpened; }
            set
            {
                if (value)
                {
                    State = GameState.StateOpened;
                }
            }
        }
        public bool Closed
        {
            get { return State == GameState.StateClosed; }
            set
            {
                if (value)
                {
                    State = GameState.StateClosed;
                }
            }
        }
        public bool BoardPreparing
        {
            get { return State == GameState.StateBoardSetup; }
            set
            {
                if (value)
                {
                    State = GameState.StateBoardSetup;
                }
            }
        }
        public Dictionary<Player, Fleet> Navies { get; set; }

    }

    public enum GameState
    {
        StateOpened = 0,
        StateBoardSetup,
        StatePlay,
        StatePause,
        StateClosed
    }
}
